﻿<cfsilent>
	<cfscript>
	
		jsonAdvice = getProperty("serviceFactory").getBean("jsonAdvice");
		response = structNew();
		
		transaction {
			
			sql = "	SELECT
						c.classroom,
						c.sits,
						c.sits_course,
						c.sits_exam,
						c.cr_type
					FROM
						t_classroom c
					WHERE
						c.cr_id = :classroomId ";
			
			queryObj = new Query(datasource = application.dnsMaster);
			queryObj.addParam(name = "classroomId", value = event.getArg('CRID'), cfsqltype = "cf_sql_char");
			rs_clsroomEntity = queryObj.execute(sql = sql).getResult();
			
			if ( event.isArgDefined("Name") ) {
				classroomName = event.getArg("Name");
			}else{
				classroomName = rs_clsroomEntity.classroom;
			}
			
			if ( event.isArgDefined("SitCount") and isNumeric( event.getArg("SitCount") ) ) {
				sitCount = event.getArg("SitCount");
			}else{
				sitCount = rs_clsroomEntity.sits;
			}
			
			if ( event.isArgDefined("SitForCourse") and isNumeric( event.getArg("SitForCourse") ) ) {
				sitForCourse = event.getArg("SitForCourse");
			}else{
				sitForCourse = rs_clsroomEntity.sits_course;
			}
			
			if ( event.isArgDefined("SitForExam") and isNumeric( event.getArg("SitForExam") ) ) {
				sitForExam = event.getArg("SitForExam");
			}else{
				sitForExam = rs_clsroomEntity.sits_exam;
			}
			
			if ( event.isArgDefined("ClassroomType") ) {
				classroomType = event.getArg("ClassroomType");
			}else{
				classroomType = rs_clsroomEntity.cr_type;
			}
			
			sql = "	UPDATE
						t_classroom c
					SET 
						c.classroom = :classroomName ,
						c.sits = :sitCount ,
						c.sits_course = :sitForCourse ,
						c.sits_exam = :sitForExam ,
						c.cr_type = :classroomType 
					WHERE
						c.cr_id = :classroomId ";
			
			queryObj = new Query(datasource = application.dnsMaster);
			queryObj.addParam(name = "classroomId", value = event.getArg('CRID'), cfsqltype = "cf_sql_char");
			queryObj.addParam(name = "classroomName", value = classroomName, cfsqltype = "cf_sql_varchar");
			queryObj.addParam(name = "sitCount", value = sitCount, cfsqltype = "cf_sql_interger");
			queryObj.addParam(name = "sitForCourse", value = sitForCourse, cfsqltype = "cf_sql_interger");
			queryObj.addParam(name = "sitForExam", value = sitForExam, cfsqltype = "cf_sql_interger");
			queryObj.addParam(name = "classroomType", value = classroomType, cfsqltype = "cf_sql_char");
			queryObj.execute(sql = sql);

			sql = "	SELECT
						c.classroom,
						c.sits,
						c.sits_course,
						c.sits_exam,
						c.cr_type,
            			ct.cr_type_name
					FROM
						t_classroom c
           				INNER JOIN t_classroom_type ct ON c.cr_type = ct.cr_type
					WHERE
						c.cr_id = :classroomId ";
			
			queryObj = new Query(datasource = application.dnsMaster);
			queryObj.addParam(name = "classroomId", value = event.getArg('CRID'), cfsqltype = "cf_sql_char");
			rs_clsroomEntity = queryObj.execute(sql = sql).getResult();
			
			classroom = structNew();
			structInsert(classroom, "ClassroomID", event.getArg('CRID'));		
			structInsert(classroom, "Name", classroomName);		
			structInsert(classroom, "SitCount", sitCount);		
			structInsert(classroom, "SitForCourse", sitForCourse);		
			structInsert(classroom, "SitForExam", sitForExam);		
		
			type = structNew();		
			structInsert(type, "Type", classroomType);		
			structInsert(type, "TypeName", rs_clsroomEntity.cr_type_name);	
		
			structInsert(response, "classroom", classroom);		
			structInsert(response, "ClassroomType", type);		
			
		}
		
	</cfscript>
</cfsilent><cfoutput>#jsonAdvice.encode(data:response, stringNumbers:true)#</cfoutput>